package com.ftg.midamall.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ftg.midamall.comm.tool.TokenUtil;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.ConstUtil;
import com.ftg.midamall.service.UserCenterService;
import com.ftg.midamall.comm.vo.RespMessUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *  用户认证中心的方法
 * 包含管理员登陆方法
 * 会员登陆方法
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/10/19 10:47
 */
@RestController
@RequestMapping("/usercenter")
@Api(tags = "认证中心")
public class UserCenterController {

    @Resource
    UserCenterService userCenterService;

    @PostMapping("/admin/login")
    @ApiOperation("管理员后台登陆功能")
    public RespMessUtil<String> adminLogin(
            @ApiParam(name = "username",value = "用户名",example = "admin" )@RequestParam(value = "username") String username,
            @ApiParam(name = "password",value = "密码",example = "123" )@RequestParam(value = "password") String password
    ){
        return userCenterService.accessUmsAdminLogin(username,password);
    }

    @PostMapping("/admin/getUserInfo")
    @ApiOperation("后台登陆成功以后获取登陆用户信息方法")
    public RespMessUtil<JSONObject> getUserInfo(
            @ApiParam(name = ConstUtil.TOKEN_NAME,value = ConstUtil.TOKEN_NAME_ZH,required = true) @RequestHeader(value = ConstUtil.TOKEN_NAME) String token
    ){
        JSONObject userinfo =null;
        try{
            userinfo = TokenUtil.parseTokenWithObject(token);
            System.out.println(userinfo);
            return RespMessUtil.build(CodeStatusEnum.SUCCESS,userinfo);
        }catch (Exception e){
            return RespMessUtil.build(CodeStatusEnum.INVALID_TOKEN);
        }
    }

    @GetMapping("/admin/demo")
    @ApiOperation("测试 方法")
    public RespMessUtil<String> logout(
    ){
        return RespMessUtil.build(CodeStatusEnum.SUCCESS,"我是10001里面的方法");
    }



}
